////
////  ZHEventStore.m
////  ZHUIKit
////
////  Created by green on 2018/3/29.
////
//
//#import "ZHEventStore.h"
//#import <ZHUIKit/UIAlertController+ZHHelper.h>
//#import <BCFoundation/BCFoundationUtils.h>
//
//#import <EventKit/EKEventStore.h>
//#import <EventKit/EKEvent.h>
//#import <EventKit/EKAlarm.h>
//#import <EventKit/EKStructuredLocation.h>
//#import <EventKit/EKReminder.h>
//#import <EventKitUI/EventKitUI.h>
//
//#import <ZHUIKit/UIViewController+ZHPage.h>
//
//
//
//@interface ZHEventStore() <EKEventEditViewDelegate>
//@property (copy, nonatomic) void(^zhAddCalendarBlock)(BOOL success);
//@end
//
//
//@implementation ZHEventStore
//
//
//
//#pragma mark - 日历、提醒
//
//+(void)zh_addCalendarEvent:(NSDate *)date title:(NSString *)title addr:(NSString *)addr remark:(NSString *)remark completion:(void(^)(BOOL success) )completion
//{
//    void(^zhAddEventNotifBlock)(BOOL support, BOOL authorized) = ^(BOOL support, BOOL authorized) {
//        if (authorized) { //授权是否成功
//            EKEventStore *eventDB = [[EKEventStore alloc] init];
//            EKEvent *myEvent  = [EKEvent eventWithEventStore:eventDB]; //创建一个日历事件
//            myEvent.title     = title;  //标题
//            myEvent.notes = remark;
//            myEvent.startDate = date; //开始date   required
//            myEvent.endDate   = date;  //结束date    required
//            //            myEvent.calendar = [EKCalendar calendarForEntityType:EKEntityTypeReminder eventStore:eventDB];
//            if (addr.length>0) {//位置
//                EKStructuredLocation *location = [EKStructuredLocation locationWithTitle:addr];
//                if (@available(iOS 9.0, *)) {
//                    myEvent.structuredLocation = location;
//                } else {
//                    // Fallback on earlier versions
//                }
//            }
//            [myEvent addAlarm:[EKAlarm alarmWithAbsoluteDate:date]]; //添加一个闹钟  optional
//            [myEvent setCalendar:[eventDB defaultCalendarForNewEvents]]; //添加calendar  required
//            NSError *err;
//            [eventDB saveEvent:myEvent span:EKSpanThisEvent error:&err]; //保存
//            if( err) {
//                if (completion) {
//                    completion(NO);
//                }
//            } else {
//                if (completion) {
//                    completion(YES);
//                }
//            }
//        } else {
//            if (completion) {
//                completion(NO);
//            }
//        }
//    };
//    EKAuthorizationStatus ekstatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
//    switch (ekstatus) {
//        case EKAuthorizationStatusAuthorized://允许
//        {
//            if(zhAddEventNotifBlock){
//                zhAddEventNotifBlock(YES, YES);
//            }
//        }
//            break;
//        case EKAuthorizationStatusNotDetermined://未决定状态
//        {
//            EKEventStore *eventDB = [[EKEventStore alloc] init];
//            [eventDB requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError * _Nullable error) {
//                dispatch_main_async_bcsafe(^{
//                    if(zhAddEventNotifBlock){
//                        zhAddEventNotifBlock(YES, granted);
//                    }
//                });
//            }];
//        }
//            break;
//        case EKAuthorizationStatusRestricted:
//        case EKAuthorizationStatusDenied://拒绝
//            default:
//        {
//            NSURL *settingUrl = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
//            BOOL canOpenSetting = [[UIApplication sharedApplication] canOpenURL:settingUrl];
//            if (canOpenSetting) {
//                //可以打开设置
//                [UIAlertController bc_showAlert:BCLOC(@"提示") message:BCLOC(@"请在 iPhone 的[设置]-[隐私]-[日历]选项中，允许攒局 APP 访问你的日历") actionBlock:^(NSInteger bottonIndex) {
//                    if(bottonIndex>0){
//                        [[UIApplication sharedApplication] openURL:settingUrl];
//                    }
//                } cancelButtonTitle:BCLOC(@"取消") otherButtonTitles:BCLOC(@"设置"),nil];
//            }
//            else{
//                //不可以打开设置
//                [UIAlertController bc_showAlert:BCLOC(@"提示") message:BCLOC(@"请在 iPhone 的[设置]-[隐私]-[日历]选项中，允许攒局 APP 访问你的日历") actionBlock:nil cancelButtonTitle:BCLOC(@"知道了") otherButtonTitles:nil];
//            }
//            if(zhAddEventNotifBlock){
//                zhAddEventNotifBlock(YES, NO);
//            }
//        }
//            break;
//    }
//}
//
//
//- (void)zh_openCalendarUI:(NSDate *)date title:(NSString *)title addr:(NSString *)addr remark:(NSString *)remark completion:(void(^)(BOOL success) )completion
//{
//    self.zhAddCalendarBlock  = completion;
//    void(^zhAddCalendarUINotifBlock)(BOOL support, BOOL authorized) = ^(BOOL support, BOOL authorized) {
//        if (authorized) { //授权是否成功
//            EKEventStore *eventDB = [[EKEventStore alloc] init];
//            UIViewController *topPage = [UIViewController bc_topPage];
//            EKEventEditViewController *calendarVC = [[EKEventEditViewController alloc] init];
//            calendarVC.eventStore = eventDB;
//            calendarVC.editViewDelegate = self;
//            EKEvent *myEvent  = [EKEvent eventWithEventStore:eventDB]; //创建一个日历事件
//            myEvent.title     = title;  //标题
//            myEvent.notes = remark;
//            myEvent.startDate = date; //开始date   required
//            myEvent.endDate   = [date dateByAddingTimeInterval:600];  //结束date    required
//            //            myEvent.calendar = [EKCalendar calendarForEntityType:EKEntityTypeReminder eventStore:eventDB];
//            if (addr.length>0) {//位置
//                EKStructuredLocation *location = [EKStructuredLocation locationWithTitle:addr];
//                if (@available(iOS 9.0, *)) {
//                    myEvent.structuredLocation = location;
//                } else {
//                    // Fallback on earlier versions
//                }
//            }
//            [myEvent addAlarm:[EKAlarm alarmWithAbsoluteDate:date]]; //添加一个闹钟  optional
//            [myEvent setCalendar:[eventDB defaultCalendarForNewEvents]]; //添加calendar  required
//            calendarVC.event = myEvent;
//            [topPage presentViewController:calendarVC animated:YES completion:nil];
//        } else {
//            if (completion) {
//                completion(NO);
//            }
//        }
//    };
//    EKAuthorizationStatus ekstatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
//    switch (ekstatus) {
//        case EKAuthorizationStatusAuthorized://允许
//        {
//            if(zhAddCalendarUINotifBlock){
//                zhAddCalendarUINotifBlock(YES, YES);
//            }
//        }
//            break;
//        case EKAuthorizationStatusNotDetermined://未决定状态
//        {
//            EKEventStore *eventDB = [[EKEventStore alloc] init];
//            [eventDB requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError * _Nullable error) {
//                dispatch_main_async_bcsafe(^{
//                    if(zhAddCalendarUINotifBlock){
//                        zhAddCalendarUINotifBlock(YES, granted);
//                    }
//                });
//            }];
//        }
//            break;
//        case EKAuthorizationStatusRestricted:
//        case EKAuthorizationStatusDenied://拒绝
//            default:
//        {
//            NSURL *settingUrl = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
//            BOOL canOpenSetting = [[UIApplication sharedApplication] canOpenURL:settingUrl];
//            if (canOpenSetting) {
//                //可以打开设置
//                [UIAlertController bc_showAlert:BCLOC(@"提示") message:BCLOC(@"请在 iPhone 的[设置]-[隐私]-[日历]选项中，允许攒局 APP 访问你的日历") actionBlock:^(NSInteger bottonIndex) {
//                    if(bottonIndex>0){
//                        [[UIApplication sharedApplication] openURL:settingUrl];
//                    }
//                } cancelButtonTitle:BCLOC(@"取消") otherButtonTitles:BCLOC(@"设置"),nil];
//            }
//            else{
//                //不可以打开设置
//                [UIAlertController bc_showAlert:BCLOC(@"提示") message:BCLOC(@"请在 iPhone 的[设置]-[隐私]-[日历]选项中，允许攒局 APP 访问你的日历") actionBlock:nil cancelButtonTitle:BCLOC(@"知道了") otherButtonTitles:nil];
//            }
//            if(zhAddCalendarUINotifBlock){
//                zhAddCalendarUINotifBlock(YES, NO);
//            }
//        }
//            break;
//    }
//
//}
//
//+(void)zh_addReminder:(NSDate *)date title:(NSString *)title addr:(NSString *)addr remark:(NSString *)remark completion:(void(^)(BOOL success) )completion
//{
//    void(^zhAddReminderNotifBlock)(BOOL support, BOOL authorized) = ^(BOOL support, BOOL authorized) {
//        if (authorized) { //授权是否成功
//            //创建一个提醒功能
//            EKEventStore *eventDB = [[EKEventStore alloc] init];
//            EKReminder *reminder = [EKReminder reminderWithEventStore:eventDB];
//            reminder.title = title;//标题
//            reminder.notes = remark;//备注
//            [reminder setCalendar:[eventDB defaultCalendarForNewReminders]];
//            NSCalendar *cal = [NSCalendar currentCalendar];
//            [cal setTimeZone:[NSTimeZone systemTimeZone]];
//            NSInteger flags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
//            NSDateComponents* dateComp = [cal components:flags fromDate:date];
//            dateComp.timeZone = [NSTimeZone systemTimeZone];
//            reminder.startDateComponents = dateComp; //开始时间
//            reminder.dueDateComponents = dateComp; //到期时间
//            reminder.priority = 1; //优先级
//            if (addr.length>0) {//位置
//                reminder.location = addr;
//            }
//            EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:date]; //添加一个车闹钟
//            [reminder addAlarm:alarm];
//            NSError *err;
//            [eventDB saveReminder:reminder commit:YES error:&err];
//            if( err) {
//                if (completion) {
//                    completion(NO);
//                }
//            } else {
//                if (completion) {
//                    completion(YES);
//                }
//            }
//        } else {
//            if (completion) {
//                completion(NO);
//            }
//        }
//    };
//    EKAuthorizationStatus ekstatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeReminder];
//    switch (ekstatus) {
//        case EKAuthorizationStatusAuthorized://允许
//        {
//            if(zhAddReminderNotifBlock){
//                zhAddReminderNotifBlock(YES, YES);
//            }
//        }
//            break;
//        case EKAuthorizationStatusNotDetermined://未决定状态
//        {
//            EKEventStore *eventDB = [[EKEventStore alloc] init];
//            [eventDB requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError * _Nullable error) {
//                dispatch_main_async_bcsafe(^{
//                    if(zhAddReminderNotifBlock){
//                        zhAddReminderNotifBlock(YES, granted);
//                    }
//                });
//            }];
//        }
//            break;
//        case EKAuthorizationStatusRestricted:
//        case EKAuthorizationStatusDenied://拒绝
//            default:
//        {
//            NSURL *settingUrl = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
//            BOOL canOpenSetting = [[UIApplication sharedApplication] canOpenURL:settingUrl];
//            if (canOpenSetting) {
//                //可以打开设置
//                [UIAlertController bc_showAlert:BCLOC(@"提示") message:BCLOC(@"请在iPhone的“设置-隐私-日历”选项中，允许程序访问你的提醒事项") actionBlock:^(NSInteger bottonIndex) {
//                    if(bottonIndex>0){
//                        [[UIApplication sharedApplication] openURL:settingUrl];
//                    }
//                } cancelButtonTitle:BCLOC(@"取消") otherButtonTitles:BCLOC(@"设置"),nil];
//            }
//            else{
//                //不可以打开设置
//                [UIAlertController bc_showAlert:BCLOC(@"提示") message:BCLOC(@"请在iPhone的“设置-隐私-日历”选项中，允许程序访问你的提醒事项") actionBlock:nil cancelButtonTitle:BCLOC(@"知道了") otherButtonTitles:nil];
//            }
//            if(zhAddReminderNotifBlock){
//                zhAddReminderNotifBlock(YES, NO);
//            }
//        }
//            break;
//    }
//}
//
//#pragma mark - EKEventEditViewDelegate
//-(void)eventEditViewController:(EKEventEditViewController *)controller didCompleteWithAction:(EKEventEditViewAction)action
//{
//    [controller dismissViewControllerAnimated:YES completion:nil];
//    if (action == EKEventEditViewActionSaved) {
//        if (self.zhAddCalendarBlock) {
//            self.zhAddCalendarBlock(YES);
//        }
//    }
//}
//
//@end
